'''

1.微信打开连接：http://redpage231109.read.biwuzhaojin.com/web-read/read?channelId=420686
2.按照提示流程转跳到浏览器，复制链接中的readUserId参数和sign，填写变量
3.青龙配置文件，添加本脚本环境变量
填写变量参数时为方便填写可以随意换行
单账户：export xzyd="['readUserId|sign']"
多账户：export xzyd="[
'1111|7a4867b4f96xxxx',
'xxxx|xxxx',
'xxxx|xxxx'
]"
4.一天运行一次
'''

import random
import time
import json
import os
import requests
import re
def getinfo(link):
    try:
        r=requests.get(link)
        #printjson(r.text)
        html = re.sub('\s', '', r.text)
        biz=re.findall('varbiz="(.*?)"\|\|', html)
        if biz!=[]:
            biz=biz[0]
        if biz=='' or biz==[]:
            if '__biz' in link:
                biz = re.findall('__biz=(.*?)&', link)
                if biz != []:
                    biz = biz[0]
        nickname = re.findall('varnickname=htmlDecode\("(.*?)"\);', html)
        if nickname!=[]:
            nickname=nickname[0]
        user_name = re.findall('varuser_name="(.*?)";', html)
        if user_name!=[]:
            user_name=user_name[0]
        msg_title = re.findall("varmsg_title='(.*?)'\.html\(", html)
        if msg_title!=[]:
            msg_title=msg_title[0]
        oriCreateTime = re.findall("varoriCreateTime='(.*?)';", html)
        if oriCreateTime != []:
            oriCreateTime = oriCreateTime[0]
        createTime = re.findall("varcreateTime='(.*?)';", html)
        if createTime != []:
            createTime = createTime[0]
        text = f'公众号唯一标识：{biz}|文章:{msg_title}|作者:{nickname}|账号:{user_name}|文章时间戳:{oriCreateTime}|文章时间:{createTime}'
        print(text)
        return nickname, user_name, msg_title, text, biz, oriCreateTime, createTime
    except Exception as e:
        print(e)
        print('异常')
        return False
class XZYD:
    def __init__(self,cg):
        self.userId=cg[0]
        self.sign=cg[1]
        self.headers={
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 NetType/WIFI MicroMessenger/7.0.20.1781(0x6700143B) WindowsWechat(0x6309080f) XWEB/8461 Flue',
            'Accept': '*/*',
            'Referer': 'http://sc1202100021.txhutl0bv.cn/',
            'Accept-Encoding': 'gzip, deflate',
            'Accept-Language': 'zh-CN,zh;q=0.9',
        }
    def info(self):
        headers={
            'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
            'Host': 'redpage231109.read.biwuzhaojin.com',
            'Origin': 'http://redpage231109.read.biwuzhaojin.com',
            'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Mobile Safari/537.36 Edg/119.0.0.0',
        }
        u='http://redpage231109.read.biwuzhaojin.com/web-read/read-info'
        p=f'channelId=1&readUserId={self.userId}&sign={self.sign}'
        r=requests.post(u,headers=headers,data=p)
        rj=r.json()
        if rj.get('code')==200:
            walletMoney=rj['data']['walletMoney']
            self.readNum = rj['data']['readNum']
            print(self.userId,f'剩余阅读次数{self.readNum},余额{walletMoney}')
        else:
            print(self.userId, f'信息获取失败只能做阅读任务')
            self.readNum=30
    def get_url(self):
        u=f'http://red231109.read.biwuzhaojin.com/web-read/get-url?readUserId={self.userId}'
        r = requests.get(u, headers=self.headers)
        #print(r.json())
        print('获取阅读连接成功')
    def ch(self):
        u=f'http://gcold.youhujia.top/dxyuedu/?ch=3521&callbackUrl=http%3A%2F%2Fred231109.read.biwuzhaojin.com%2Fweb-read%2Fget-url%3FreadUserId%3D{self.userId}&t={int(time.time()*1000)}&rbid=11111&cb=w&host=sc1202100021.txhutl0bv.cn'
        r = requests.get(u, headers=self.headers)
        rt=r.text
        nrt=rt.replace('w(','').replace(')','')
        rj=json.loads(nrt)
        url=rj.get('url')
        if url:
            getinfo(url)
        else:
            print('阅读完成')
    def read(self):
        for i in range(1,int(self.readNum)+1):
            print('-'*50)
            print('第一次阅读')
            self.get_url()
            if self.ch()==False:return False
            time.sleep(random.randint(6,8))
            print('阅读成功')
            self.info()
    def tx(self):
        print('功能开发中')
    def run(self):
        self.info()
        self.read()
        self.tx()

def getEnv(en):
    env = os.getenv(en)
    if env == None:
        print(f'{en}青龙变量里没有获取到，使用本地参数')
        return False
    try:
        env = json.loads(env.replace("'", '"').replace("\n", "").replace(" ", "").replace("\t", ""))
        return env
    except Exception as e:
        print('错误:', e)
        print('你填写的变量是:', env)
        print('请检查变量参数是否填写正确')
        print(f'{en}使用本地参数')
if __name__ == '__main__':
    for i in getEnv('xzyd'):
        api=XZYD(i.split('|'))
        api.run()